import json

from clone import clone_file
from get_link import get_link
import time
from tqdm import tqdm

file_end_logo = {
    'python': ['.py'],
    'java': ['.java'],
    'javascript': ['.vue', 'js', 'ts', 'jsx', 'tsx'],
    'Python': ['.py'],
    'Java': ['.java'],
    'Javascript': ['.vue', 'js', 'ts', 'jsx', 'tsx']
}
page_max = 1
page_size = 2

link_list_total = []

error_link_list = []


with open('./get_keyword_desc_by_gpt/lang_key_desc.json', 'r', encoding='utf-8')as f:
    L = json.load(f)
    lang_key_desc_list = [lang_key_desc for lang_key_desc in L if lang_key_desc != -1 and 'language' in lang_key_desc.keys() and 'key_word' in lang_key_desc.keys() and 'description' in lang_key_desc.keys()]

lang2key_map = {
    'python': set(),
    'java': set(),
    'javascript': set()
}

for lang_key_desc in lang_key_desc_list:
    if lang_key_desc['language'] == 'python' or lang_key_desc['language'] == 'Python':
        for key in lang_key_desc['key_word']:
            lang2key_map['python'].add(key)
    elif lang_key_desc['language'] == 'java' or lang_key_desc['language'] == 'Java':
        for key in lang_key_desc['key_word']:
            lang2key_map['java'].add(key)
    else:
        for key in lang_key_desc['key_word']:
            lang2key_map['javascript'].add(key)

for lang in lang2key_map:
    end_logo_list = file_end_logo[lang]
    for key in tqdm(lang2key_map[lang]):
        for page in range(page_max):
            try:
                link_list = get_link(page, page_size, lang, '',
                                     key)
                for link in link_list:
                    if clone_file(link, end_logo_list) != 0:
                        error_link_list.append(link)
                    time.sleep(0.3)
            except Exception as e:
                print(e)

# for lang_key_desc in tqdm(lang_key_desc_list):
#     end_logo_list = file_end_logo[lang_key_desc['language']]
#     for page in range(page_max):
#         try:
#             link_list = get_link(page, page_size, lang_key_desc['language'], lang_key_desc['description'], lang_key_desc['key_word'])
#             for link in link_list:
#                 if clone_file(link, end_logo_list) != 0:
#                     error_link_list.append(link)
#                 time.sleep(0.3)
#         except Exception as e:
#             print(e)
